package org.charles.zhifou.model.vo;

import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.charles.zhifou.model.dto.question.QuestionContentDTO;
import org.charles.zhifou.model.entity.Question;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 题目视图
 *
 * @author charles
 * @version 1.0
 */
@Data
public class QuestionVO implements Serializable {
    @Schema(description = "id")
    private Long id;

    @Schema(description = "题目内容（json格式）")
    private List<QuestionContentDTO> questionContent;

    @Schema(description = "应用 id")
    private Long appId;

    @Schema(description = "创建用户 id")
    private Long userId;

    @Schema(description = "创建时间")
    private Date createTime;

    @Schema(description = "更新时间")
    private Date updateTime;

    @Schema(description = "创建用户信息")
    private UserVO user;

    /**
     * 对象转封装类
     *
     * @param question
     * @return
     */
    public static QuestionVO entityToVo(Question question) {
        if (question == null) {
            return null;
        }

        QuestionVO questionVO = new QuestionVO();
        BeanUtils.copyProperties(question, questionVO);
        // 题目需要从 JSON 字符串转为 DTO 类列表
        String questionContent = question.getQuestionContent();
        if (questionContent != null) {
            questionVO.setQuestionContent(JSONUtil.toList(questionContent, QuestionContentDTO.class));
        }

        return questionVO;
    }
}
